package cn.vshare.controller;

import cn.vshare.entity.User;
import cn.vshare.service.MessageService;
import cn.vshare.util.IpUtil;
import com.github.pagehelper.Page;
import org.apache.log4j.Logger;
import org.apache.log4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class MessageAction {

    @Autowired
    private MessageService messageService;

    private Logger log = Logger.getLogger(MessageAction.class); //引入日志类

    @RequestMapping("/forwardMemberList")
    public ModelAndView forwardMemberList(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("h-ui/member-list");
        return mv;
    }

    @RequestMapping("/forwardMemberDel")
    public ModelAndView forwardMemberDel(){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("h-ui/member-del");
        return mv;
    }


    @RequestMapping("/getMemberInfo")
    public ModelAndView getMemberInfo(String keyword, int currPage, String isForbidden, String isDestory, int pageSize, HttpServletRequest request){
        //封装用户信息
        ModelAndView mv = new ModelAndView();
        Map<String ,Object> userMap = new HashMap<String, Object>();
        mv.setView(new MappingJackson2JsonView());
        Page<User> userPage = messageService.getAllMemberUser(currPage,pageSize,keyword,isForbidden,isDestory); //获取所有符合条件的会员
        userMap.put("currPage",userPage.getPageNum());
        userMap.put("totalPage",userPage.getPages());
        userMap.put("totalRows",userPage.getTotal());
        userMap.put("userPage",userPage);
        mv.addObject(userMap);

        //记录操作日志
        MDC.put("id",UUID.randomUUID().toString()); //主键
        MDC.put("uid","2bb03abc-03d0-4115-a2c9-eef51556d6a5"); //记录操作的用户id,这里是测试,写入的固定用户
        MDC.put("ip",IpUtil.getIp(request));  //记录访问的ip地址
        log.info("获取第"+currPage+"页会员用户信息,共"+userPage.getPages()+"页"+",每页显示"+pageSize+"条");
        return mv;
    }

    @RequestMapping("/queryMember")
    public ModelAndView queryMember(String startTime, String endTime, String keyword, String condition, int currPage, int pageSize, String isForbidden, String isDestory, HttpServletRequest request){
        //封装用户信息
        ModelAndView mv = new ModelAndView();
        Map<String ,Object> userMap = new HashMap<String, Object>();
        mv.setView(new MappingJackson2JsonView());
        Page<User> userPage = messageService.getMemberByKeyword(startTime,endTime,keyword,currPage,pageSize,condition,isForbidden,isDestory);
        userMap.put("currPage",userPage.getPageNum());
        userMap.put("totalPage",userPage.getPages());
        userMap.put("totalRows",userPage.getTotal());
        userMap.put("userPage",userPage);
        mv.addObject(userMap);

        //记录操作日志
        MDC.put("id",UUID.randomUUID().toString()); //主键
        MDC.put("uid","2bb03abc-03d0-4115-a2c9-eef51556d6a5"); //记录操作的用户id
        MDC.put("ip",IpUtil.getIp(request));  //记录访问的ip地址
        log.info("执行分条件搜索方法,获取第"+currPage+"页会员用户信息,共"+userPage.getPages()+"页"+",每页显示"+pageSize+"条");
        return mv;
    }

    public MessageService getMessageService() {
        return messageService;
    }

    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }
}
